<template>
  <view>
    <text>系统登录中...</text>
    <u-picker :closeOnClickOverlay="false" mode="selector" v-model="pickerShow" @confirm="pickerConfirm" @columnchange="columnChange"
              :range="range" range-key="fullDeptName" @close="close">
    </u-picker>
  </view>
</template>

<script>
import {
  authLoginKey,
  deptChoose,
  qryUserDetails,
  ztSsoLogin
} from "@/api/public/login.js";
import {
  mapMutations
} from 'vuex';
import {
  Base64
} from "@/common/utils/base64.js";
import UPicker from "../../uview-ui/components/u-picker/u-picker";
export default {
  components: {
    UPicker
  },
  data() {
    return {
      // 部门选择弹窗
      pickerShow: false,
      range: [],
      key: '',
      sysUsersKey: '',
      routerPath:'',
      option: {}
    };
  },
  onShow() {},
  onLoad(option) {
    this.loginZt(option)
  },

  methods: {
    close() {
      return
    },
    ...mapMutations(['saveDepRange']),
    loginZt(option){
      this.option = option
      if (!option.code){
        this.$error({
          content: "单点登录失败，缺少code参数"
        });
        return
      }
      let deptId = ""
      if (option.deptId){
        deptId = option.deptId
      }
      uni.setStorageSync('token', "")
      ztSsoLogin({
        code: option.code,
        deptId: deptId
      }).then(response => {
        if (response.code == 0){
          let data = response.data;
          this.$store.commit('setHeader', {
            "Authorization": "Bearer " + data.authToken
          });
          uni.setStorageSync('token', data.authToken)
          this.setQryUserDetails(data)
        } else if (response.code == 1){
          this.sysUsersKey = response.data.flag
          this.range = response.data.userList
          this.pickerShow = true;
        } else {
          if (!option.code){
            this.$error({
              content: response.message
            });
          }
        }
      })


    },
    tokenKeyLogin(key) {
      authLoginKey({
        key: key
      }).then(response => {
        if (response.code == 0) {
          let data = response.data;
          this.$store.commit('setHeader', {
            "Authorization": "Bearer " + data.authToken
          });
          uni.setStorageSync('token', data.authToken)
          this.setQryUserDetails(data)
        } else {
          this.$error({
            content: response.data.message
          });
        }
      })
          .catch(response => {
            console.log(response);
          });
    },
    setQryUserDetails(data) {
      qryUserDetails().then(res1 => {
        if (res1.code == 0) {
          this.$store.commit("setHeader", {
            "Authorization": "Bearer " + data.authToken,
            'currentDeptName': encodeURIComponent(res1.data.deptInfo[0].deptName),
            'currentUserName': encodeURIComponent(res1.data.userInfo.userName),
            'deptName': encodeURIComponent(res1.data.bizDeptInfo.deptName)
          });
          res1.data.userInfo.deptNameList = res1.data.deptInfo[0].deptName
          console.log('res1.data.userInfo',res1.data.userInfo);
          this.$store.commit("setUserInfo", res1.data.userInfo);
          this.$store.commit('login')
          uni.setStorage([{
            key: 'deptinfo',
            data: res1.data.deptInfo
          }])
          if(this.option.dealPath && (this.option.dealPath == 'orderApproval' || this.option.dealPath == 'orderMasterApproval' )) {
            let procInstId = Base64.decode(this.option.procInstId)
            let applyId = Base64.decode(this.option.applyId)
            let workitemId = Base64.decode(this.option.undoId)
            let entityId = Base64.decode(this.option.entityId)
            // 换成reLaunch,这样不会回到单点页
            uni.reLaunch({
              url: `/pages/backlog/backlog-detail?applyId=${applyId}&procInstId=${procInstId}&workitemId=${workitemId}&entityId=${entityId}&isDone=0&ztet=1`
            })
          }else {
            uni.reLaunch({
              url: "/pages/index/index"
            })
          }

        }else {
          this.$error({
            content: res1.data.message
          });
        }
      })
    },
    columnChange(e) {},
    pickerConfirm(e) {
      const param = {
        key: this.sysUsersKey,
        userCode: this.range[e[0]].userCode
      }
      this.saveDepRange(this.range)
      deptChoose(param).then(res => {
        if (res.code === 0) {
          this.tokenKeyLogin(res.data)
        } else {
          this.$util.msg(res.message);
        }
      });
    },
  },
};
</script>

<style lang='scss'>
</style>
